//
//  StudyCalendarViewController.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/31.
//

#import "StudyCalendarViewController.h"
#import "YQCalendarView.h"
#import "ClockCalendarManager.h"
#import "StudyCalendarModel.h"

@interface StudyCalendarViewController ()<YQCalendarViewDelegate,JPAPIManagerCallBackDelegate>

@property(nonatomic,strong)YQCalendarView *calendarView;
@property(nonatomic,strong)ClockCalendarManager *calendarManager;
@property(nonatomic,strong)StudyCalendarModel *calendarModel;
@property(nonatomic,strong)UILabel *dayLabel;
@property(nonatomic,strong)UILabel *totalDayLabel;
@property(nonatomic,strong)UIView *bgView;

@end

@implementation StudyCalendarViewController

-(ClockCalendarManager *)calendarManager{
    if(!_calendarManager){
        _calendarManager = [[ClockCalendarManager alloc]init];
        _calendarManager.delegate = self;
    }
    return _calendarManager;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    [self createUI];
    [self getDataWithMonth:[self getCurrentYearMonth]];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self popGestureClose];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self popGestureOpen];
}

#pragma mark - 获取日历数据
-(void)getDataWithMonth:(NSString*)month{
    self.calendarManager.month = month;
    [self.calendarManager loadData];
}

#pragma mark - 获取当前年月
- (NSString *)getCurrentYearMonth {
    // 获取当前日期
    NSDate *currentDate = [NSDate date];
    // 创建日期格式化器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    // 将当前日期格式化为指定格式的字符串
    NSString *yearMonthString = [dateFormatter stringFromDate:currentDate];
    return yearMonthString;
}

-(void)createUI{
    
    [self.customNavigationBar setTitle:@"打卡日历"];
    
    CGFloat calendar_w = [DeviceUtils getAdaptedWidthForDesignWidth:318 andCurrentAvailableWidth:self.currentAvailableWidth];
    if((self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-16-49-16-34-74) < calendar_w){
        calendar_w = self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-16-49-16-34-74;
    }
    CGFloat c_left = 16;
    self.calendarView = [[YQCalendarView alloc]initWithFrame:CGRectMake(c_left,kYM_NAVIGATIONBAR_HEIGHT+16,self.currentAvailableWidth-c_left*2,calendar_w+49) andWidth:self.currentAvailableWidth];
    self.calendarView.delegate = self;
    [self.view addSubview:self.calendarView];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(c_left,CGRectGetMaxY(self.calendarView.frame)+16,self.currentAvailableWidth-c_left*2,74)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
    self.bgView = bgView;
    
    CGFloat d_w = (self.currentAvailableWidth-c_left*2-48)/2.0;
    NSArray *arr = @[@"累计打卡/天",@"连续打卡/天"];
    for (NSInteger i = 0; i < arr.count; i++) {
        UILabel *dayLabel = [[UILabel alloc]init];
        dayLabel.font = JPUIFontInterSemiBold(20);
        dayLabel.textColor = KFontBlackColor;
        dayLabel.textAlignment = NSTextAlignmentCenter;
        dayLabel.text = @"0";
        [bgView addSubview:dayLabel];
        [dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(d_w);
            make.height.mas_equalTo(@24);
            make.top.mas_equalTo(@12);
            make.left.mas_equalTo(12+i*(d_w+24));
        }];
        if(i == 0){
            self.totalDayLabel = dayLabel;
        }else{
            self.dayLabel = dayLabel;
        }
        
        UILabel *tipLabel = [[UILabel alloc]init];
        tipLabel.font = JPUIFontPingFangSCRegular(14);
        tipLabel.textColor = KFontGrayColor;
        tipLabel.textAlignment = NSTextAlignmentCenter;
        tipLabel.text = arr[i];
        tipLabel.tag = 100+i;
        [bgView addSubview:tipLabel];
        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(d_w);
            make.height.mas_equalTo(@20);
            make.bottom.mas_equalTo(@-12);
            make.left.mas_equalTo(12+i*(d_w+24));
        }];
        
    }
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = KLineColor;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@1);
        make.height.mas_equalTo(@34);
        make.centerY.mas_equalTo(bgView);
        make.centerX.mas_equalTo(bgView);
    }];
    
    
}

#pragma mark - 屏幕宽度改变
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    if(isIPAD){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           // 获取当前可用宽度并更新布局
          self.currentAvailableWidth = [DeviceUtils currentAvailableWidth];
          self.currentAvailableHeight = [DeviceUtils currentAvailableHeight];
          self.customNavigationBar.frame = CGRectMake(0, 0, self.currentAvailableWidth,kYM_NAVIGATIONBAR_HEIGHT);
          CGFloat calendar_w = [DeviceUtils getAdaptedWidthForDesignWidth:318 andCurrentAvailableWidth:self.currentAvailableWidth];
          if((self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-16-49-16-34-74) < calendar_w){
              calendar_w = self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-16-49-16-34-74;
          }
          CGFloat c_left = 16;
          self.calendarView.frame = CGRectMake(c_left,kYM_NAVIGATIONBAR_HEIGHT+16,self.currentAvailableWidth-c_left*2,calendar_w+49);
          self.calendarView.currentAvailableWidth = self.currentAvailableWidth;
          [self.calendarView refreshWidth];
          self.bgView.frame = CGRectMake(c_left,CGRectGetMaxY(self.calendarView.frame)+16,self.currentAvailableWidth-c_left*2,74);
          CGFloat d_w = (self.currentAvailableWidth-c_left*2-48)/2.0;
          for(NSInteger i = 0;i < 2;i++){
             if(i == 0){
                 [self.totalDayLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                     make.width.mas_equalTo(d_w);
                     make.left.mas_equalTo(12+i*(d_w+24));
                 }];
              }else{
                 [self.dayLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                     make.width.mas_equalTo(d_w);
                     make.left.mas_equalTo(12+i*(d_w+24));
                 }];
              }
              UILabel *tipLabel = [self.bgView viewWithTag:100+i];
              [tipLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                  make.width.mas_equalTo(d_w);
                  make.left.mas_equalTo(12+i*(d_w+24));
              }];
          }
         
            
            
        });
    }
}



-(void)YQCalendarViewTouchedOneDay:(NSString *)dateString{
//    NSLog(@"点击了：%@",dateString);
}

- (void)YQCalendarViewTouchedNextOrPreMonth:(NSString *)yearAndMonth{
    //点击了上一个月或者下一个月 重新获取打卡数据
    [self getDataWithMonth:yearAndMonth];
}

#pragma mark - 刷新界面
-(void)refreshUI{
    self.totalDayLabel.text = self.calendarModel.sum;
    self.dayLabel.text = self.calendarModel.seriesCount;
    self.calendarView.calendarModel = self.calendarModel;
}


#pragma mark - JPAPIManagerCallBackDelegate
-(void)managerCallDLDLAPIDidSuccess:(JPAPIBaseManager *)manager{
    
    if([manager isEqual:self.calendarManager]){
        NSDictionary *dic = manager.response.content;
        self.calendarModel = [StudyCalendarModel mj_objectWithKeyValues:dic[@"data"]];
        [self refreshUI];
    }
    
}

-(void)managerCallDLDLAPIDidFailed:(JPAPIBaseManager *)manager{
    if(manager.errorMessage){
        [JPProgressHUD showMsg:manager.errorMessage];
    }
}


@end
